package com.zhou.imaotai.controller;


import com.zhou.imaotai.entity.R;
import com.zhou.imaotai.entity.SysUserEntity;
import com.zhou.imaotai.model.LoginBody;
import com.zhou.imaotai.service.web.SysLoginService;
import com.zhou.imaotai.utils.SecurityUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author lcz
 * @desc sys登录控制器
 * @mail lcz_0130@163.com
 * @date 2024/03/26
 */

@RestController
@RequestMapping
@Api("登录路由")
public class SysLoginController {

    @Autowired
    private SysLoginService loginService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping(value = "/login", name = "登录方法")
    public R login(@RequestBody LoginBody loginBody) {
        R r = R.ok();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        r.put("token", token);
        return r;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping(value = "getInfo", name = "获取用户信息")
    public R getInfo() {
        SysUserEntity user = SecurityUtils.getLoginUser().getUser();
        R ajax = R.ok();
        ajax.put("user", user);
        return ajax;
    }
}
